#ifndef _LOG_H_
#define _LOG_H_

#include <stdarg.h>
#include <stdio.h>
#include <time.h>

#define COLOR_RED              "\x1b[31m"
#define COLOR_GREEN            "\x1b[32m"
#define COLOR_YELLOW           "\x1b[33m"
#define COLOR_BLUE             "\x1b[34m"
#define COLOR_RESET            "\x1b[0m"

#define LOG_ERROR(format, ...) logMessage(COLOR_RED "ERROR", __FILE__, __LINE__, format, ##__VA_ARGS__)
#define LOG_DEBUG(format, ...) logMessage(COLOR_BLUE "DEBUG", __FILE__, __LINE__, format, ##__VA_ARGS__)
#define LOG_INFO(format, ...)  logMessage(COLOR_GREEN "INFO", __FILE__, __LINE__, format, ##__VA_ARGS__)

static void getCurrentTime(char* buffer, size_t bufferSize) {
    time_t t;
    struct tm* tm_info;

    time(&t);
    tm_info = localtime(&t);

    strftime(buffer, bufferSize, "%Y-%m-%d %H:%M:%S", tm_info);
}

static void logMessage(const char* level, const char* file, int line, const char* format, ...) {
    char timeBuffer[20];
    getCurrentTime(timeBuffer, sizeof(timeBuffer));

    va_list args;
    va_start(args, format);

    fprintf(stderr, "%s [%s] %s:%d ", timeBuffer, level, file, line);
    vfprintf(stderr, format, args);
    fprintf(stderr, COLOR_RESET "\n");

    va_end(args);
}

#endif